package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.IncomeAndExpensesBean;
import com.wnhz.work.jy.fragment.ExpensesFragment;
import com.wnhz.work.jy.fragment.IncomeFragment;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---收支明细
 */
public class IncomeAndExpensesDetailActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "IncomeAndExpensesDetailActivity";
    private TextView title;

    private TextView tv_income, tv_expenses;
    private View line1, line2;

    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private IncomeFragment fragment1;
    private ExpensesFragment fragment2;

    private int textColorNor, textColorCli;

    public IncomeAndExpensesBean incomeAndExpensesBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_and_expenses_detail);

        initData();
        initView();
    }

    private void initData() {
        textColorNor = getResources().getColor(R.color.text);
        textColorCli = getResources().getColor(R.color.main_color);
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("资金明细");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        tv_income = (TextView) findViewById(R.id.tv_income);
        tv_expenses = (TextView) findViewById(R.id.tv_expenses);
        tv_income.setOnClickListener(this);
        tv_expenses.setOnClickListener(this);

        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);

        fragmentList = new ArrayList<>();
        fragment1 = new IncomeFragment();
        fragment2 = new ExpensesFragment();
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);

        fragmentManager = getFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_detail, fragment1);
        transaction.add(R.id.fl_detail, fragment2);

        transaction.hide(fragment2);
        transaction.commit();
        tv_income.setTextColor(textColorCli);
        line1.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onResume() {
        getDate();
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        hideFragment();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //收入
            case R.id.tv_income:
                transaction.show(fragmentList.get(0)).commit();
                tv_income.setTextColor(textColorCli);
                tv_expenses.setTextColor(textColorNor);
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                break;

            //转出
            case R.id.tv_expenses:
                transaction.show(fragmentList.get(1)).commit();
                tv_income.setTextColor(textColorNor);
                tv_expenses.setTextColor(textColorCli);
                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                break;

            default:
                break;
        }
    }

    private void hideFragment() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            transaction.hide(fragmentList.get(i));
        }
        transaction.commit();
    }

    private void getDate(){
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.IN_OUT_DETAIL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.d(TAG, "onSuccess: "+result);
                incomeAndExpensesBean = new Gson().fromJson(result,IncomeAndExpensesBean.class);
                fragment1.setData();
                fragment2.setData();
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

}
